package Test;

import javax.swing.*;
import java.awt.*;

public class JScrollPaneTest {
    private  static void JScrollPane(){

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setSize(400,300);
        frame.setLocation(200,300);

        JScrollPane scrollPane = new JScrollPane();

        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        
        JPanel panel = new JPanel();
        panel.add(new JButton("按钮1"));
        panel.add(new JButton("按钮2"));
        panel.add(new JButton("按钮3"));
        panel.add(new JButton("按钮4"));
        panel.add(new JButton("按钮5"));
        panel.add(new JButton("按钮6"));
        panel.add(new JButton("按钮7"));
        panel.add(new JButton("按钮8"));
        scrollPane.setViewportView(panel);
        frame.add(scrollPane, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(JScrollPaneTest::JScrollPane);
    }
}
